package top.hcode.hoj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.dto.*;
import top.hcode.hoj.pojo.entity.course.*;
import top.hcode.hoj.pojo.vo.CourseSectionVO;
import top.hcode.hoj.pojo.vo.ExamVO;
import top.hcode.hoj.pojo.vo.QuestionVO;
import top.hcode.hoj.service.admin.AdminExamService;
import top.hcode.hoj.service.admin.course.AdminCourseSectionService;
import top.hcode.hoj.service.admin.course.AdminCourseService;
import top.hcode.hoj.service.admin.course.AdminLessonService;
import top.hcode.hoj.service.admin.course.AdminQuestionService;
import top.hcode.hoj.service.admin.tag.AdminTagService;
import top.hcode.hoj.utils.ShiroUtils;

import java.util.List;


/**
 * @Author: orangej
 * @Date: 2024-12-18
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminCourseController {

    @Autowired
    private AdminCourseService courseService;
    @Autowired
    private AdminCourseSectionService sectionService;
    @Autowired
    private AdminLessonService lessonService;
    @Autowired
    private AdminExamService examService;
    @Autowired
    private AdminQuestionService questionService;
    @Autowired
    private AdminTagService tagService;

    /* start 课程 */
    @ApiOperationSupport(order = 1)
    @GetMapping("/course/list")
    @ApiOperation(value = "获取课程列表", notes = "获取课程列表")
    public CommonResult<IPage<Course>> getCourseList(AdminQueryCourseDTO queryCourseDto) {
        return CommonResult.successResponse(courseService.getCourseList(queryCourseDto));
    }

    @PostMapping("/course/save")
    @ApiOperation(value = "保存课程，支持新建和更新")
    public CommonResult<Course> saveCourse(@Validated @RequestBody Course course) {
        course.setUid(ShiroUtils.getUid());
        return CommonResult.successResponse(courseService.saveCourse(course));
    }

    @PostMapping("/course/delete")
    @ApiOperation(value = "删除课程")
    public CommonResult<Void> deleteCourse(@Validated @RequestBody BaseIdDTO dto) {
        courseService.deleteCourse(dto.getId());
        return CommonResult.successResponse();
    }
    /* end 课程 */

    /* start 章节 */
    @GetMapping("/course-section/list")
    @ApiOperation(value = "获取章节列表")
    public CommonResult<IPage<CourseSection>> getCourseSectionList(AdminQueryCourseSectionDTO dto) {
        return CommonResult.successResponse(sectionService.getCourseSectionList(dto));
    }

    @PostMapping("/course-section/save")
    @ApiOperation(value = "保存章节，支持新建和更新")
    public CommonResult<CourseSection> saveCourseSection(@Validated @RequestBody CourseSection courseSection) {
        return CommonResult.successResponse(sectionService.saveCourseSection(courseSection));
    }

    @GetMapping("/course-section/detail")
    @ApiOperation(value = "获取章节详情")
    public CommonResult<CourseSectionVO> getSectionDetail(BaseIdDTO dto) {
        return CommonResult.successResponse(sectionService.getSectionDetail(dto.getId()));
    }

    @PostMapping("/course-section/delete")
    @ApiOperation(value = "删除章节")
    public CommonResult<Void> deleteCourseSection(@Validated @RequestBody BaseIdDTO dto) {
        sectionService.deleteCourseSection(dto.getId());
        return CommonResult.successResponse();
    }

    @PostMapping("/course-section/sort")
    @ApiOperation(value = "保存章节排序")
    public CommonResult<Void> saveCourseSectionSort(@Validated @RequestBody CourseSectionSortDTO dto) {
        sectionService.saveCourseSectionSort(dto);
        return CommonResult.successResponse();
    }
    /* end 章节 */

    /* start 课时 */
    @GetMapping("/lesson/list")
    @ApiOperation(value = "获取课时列表")
    public CommonResult<IPage<Lesson>> getLessonList(AdminQueryLessonDTO dto) {
        return CommonResult.successResponse(lessonService.getLessonList(dto));
    }

    @GetMapping("/lesson/detail")
    @ApiOperation(value = "获取课时详情")
    public CommonResult<Lesson> getLessonDetail(BaseIdDTO dto) {
        return CommonResult.successResponse(lessonService.getLessonDetail(dto.getId()));
    }

    @PostMapping("/lesson/save")
    @ApiOperation(value = "保存课时，支持新建和更新")
    public CommonResult<Lesson> saveLesson(@Validated @RequestBody Lesson lesson) {
        return CommonResult.successResponse(lessonService.saveLesson(lesson));
    }

    @PostMapping("/lesson/delete")
    @ApiOperation(value = "删除课时")
    public CommonResult<Void> deleteLesson(@Validated @RequestBody BaseIdDTO dto) {
        lessonService.deleteLesson(dto.getId());
        return CommonResult.successResponse();
    }

    @PostMapping("/lesson-handout/save")
    @ApiOperation(value = "保存讲义")
    public CommonResult<LessonHandout> saveHandout(@Validated @RequestBody LessonHandout handout) {
        return CommonResult.successResponse(lessonService.saveHandout(handout));
    }

    @PostMapping("/lesson-handout/delete")
    @ApiOperation(value = "删除讲义")
    public CommonResult<Void> deleteHandout(@Validated @RequestBody BaseIdDTO dto) {
        lessonService.deleteHandout(dto.getId());
        return CommonResult.successResponse();
    }
    /* end 课时 */

    /* start 测验 */
    @GetMapping("/exam/list")
    @ApiOperation(value = "获取测验列表")
    public CommonResult<IPage<ExamVO>> getExamList(QueryExamDTO dto) {
        return CommonResult.successResponse(examService.getExamList(dto));
    }

    @GetMapping("/exam/detail")
    @ApiOperation(value = "获取测验详情")
    public CommonResult<ExamVO> getExamDetail(BaseIdDTO dto) {
        return CommonResult.successResponse(examService.getExamDetail(dto.getId()));
    }

    @PostMapping("/exam/save")
    @ApiOperation(value = "保存测验，支持新建和更新")
    public CommonResult<Exam> saveExam(@Validated @RequestBody Exam exam) {
        return CommonResult.successResponse(examService.saveExam(exam));
    }

    @PostMapping("/exam/delete")
    @ApiOperation(value = "删除测验")
    public CommonResult<Void> deleteExam(@Validated @RequestBody BaseIdDTO dto) {
        examService.deleteExam(dto.getId());
        return CommonResult.successResponse();
    }


    @PostMapping("/exam-section/save")
    @ApiOperation(value = "保存测验章节，支持新建和更新")
    public CommonResult<ExamSection> saveExamSection(@Validated @RequestBody ExamSection section) {
        return CommonResult.successResponse(examService.saveExamSection(section));
    }

    @PostMapping("/exam-section/delete")
    @ApiOperation(value = "删除测验章节")
    public CommonResult<Void> deleteExamSection(@Validated @RequestBody BaseIdDTO dto) {
        examService.deleteExamSection(dto.getId());
        return CommonResult.successResponse();
    }

    @PostMapping("/exam-section/sort")
    @ApiOperation(value = "保存测验排序")
    public CommonResult<Void> saveExamSort(@Validated @RequestBody BaseIdListDTO dto) {
        examService.saveExamSort(dto);
        return CommonResult.successResponse();
    }
    /* end 测验 */

    /* start 作业 */
    @GetMapping("/lesson-work-question/list")
    @ApiOperation(value = "获取作业列表")
    public CommonResult<List<LessonWorkQuestion>> getLessonWorkList(@Validated QueryLessonWorkDTO dto) {
        return CommonResult.successResponse(lessonService.getLessonWorkList(dto));
    }

    @PostMapping("/lesson-work-question/save")
    @ApiOperation(value = "保存作业")
    public CommonResult<LessonWorkQuestion> saveLessonWork(@Validated @RequestBody LessonWorkQuestion question) {
        return CommonResult.successResponse(lessonService.saveLessonWork(question));
    }

    @PostMapping("/lesson-work-question/delete")
    @ApiOperation(value = "删除作业")
    public CommonResult<Void> deleteLessonWork(@Validated @RequestBody BaseIdDTO dto) {
        lessonService.deleteLessonWork(dto.getId());
        return CommonResult.successResponse();
    }
    
    @PostMapping("/lesson-work-question/sort")
    @ApiOperation(value = "保存作业排序")
    public CommonResult<Void> saveLessonWorkSort(@Validated @RequestBody BaseIdListDTO dto) {
        lessonService.saveLessonWorkSort(dto);
        return CommonResult.successResponse();
    }
    /* end 作业 */

    /* start 习题 */
    @GetMapping("/question/list")
    @ApiOperation(value = "获取习题列表")
    public CommonResult<IPage<QuestionVO>> getQuestionList(QueryQuestionDTO dto) {
        return CommonResult.successResponse(questionService.getQuestionList(dto));
    }

    @GetMapping("/question/detail")
    @ApiOperation(value = "获取习题详情")
    public CommonResult<QuestionVO> getQuestionDetail(BaseIdDTO dto) {
        return CommonResult.successResponse(questionService.getQuestionDetail(dto.getId()));
    }

    @PostMapping("/question/save")
    @ApiOperation(value = "保存习题，支持新建和更新")
    public CommonResult<Question> saveQuestion(@Validated @RequestBody SaveQuestionDTO question) {
        return CommonResult.successResponse(lessonService.saveQuestion(question));
    }

    @PostMapping("/question/delete")
    @ApiOperation(value = "删除习题")
    public CommonResult<Void> deleteQuestion(@Validated @RequestBody BaseIdDTO dto) {
        questionService.deleteQuestion(dto.getId());
        return CommonResult.successResponse();
    }
    /* end 习题 */

    @PostMapping("/tag-rel/save")
    @ApiOperation(value = "保存标签关联关系")
    public CommonResult<Void> saveTagRel(@Validated @RequestBody SaveTagRelDTO tagRel) {
        return CommonResult.successResponse(tagService.saveTagRel(tagRel));
    }
}